package rjeutsky.models.builders;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Random;

import rjeutsky.models.CardThema;
import rjeutsky.models.CardType;
import rjeutsky.models.TypeEnum;

public class RandomOldCardBuilder extends OldCardBuilder {
	
	protected List<String> authors;
	protected List<String> countries;
	protected List<String> valuablities;
	protected int minYear = 0;
	protected int maxYear = new GregorianCalendar().get(Calendar.YEAR);
	protected Random generator = new Random();
	
	@Override
	public void buildAuthor() {
		if (authors.size() > 0){
			int randomIndex = generator.nextInt(authors.size()); 
			card.setAuthor(authors.get(randomIndex));
		}
	}

	@Override
	public void buildCountry() {
		if (countries.size() > 0){
			int randomIndex = generator.nextInt(countries.size());
			card.setCountry(countries.get(randomIndex));
		}
	}

	@Override
	public void buildThema() {
		if (CardThema.values().length > 0){
			int randomIndex = generator.nextInt(CardThema.values().length);
			card.setThema(CardThema.values()[randomIndex]);
		}
	}

	@Override
	public void buildType() {
		CardType cardType = new CardType();
		cardType.setIsSent(generator.nextBoolean());
		if (TypeEnum.values().length > 0){
			int randomIndex = generator.nextInt(TypeEnum.values().length);
			cardType.setType(TypeEnum.values()[randomIndex]);
		}
		card.setType(cardType);
	}

	@Override
	public void buildValuable() {
		int valuabilitiesCount = valuablities == null ? 0 : valuablities.size();
		int randomIndex = generator.nextInt(valuabilitiesCount + 1);  
		if (randomIndex != valuabilitiesCount){
			card.setValuable(valuablities.get(randomIndex));
		}
	}

	@Override
	public void buildYear() {
		if (maxYear >= minYear){
			int year = minYear + generator.nextInt(maxYear - minYear);
			card.setYear(year);
		}
	}
	
	public void setAuthors(List<String> authors) {
		this.authors = authors;
	}

	public void setCountries(List<String> countries) {
		this.countries = countries;
	}

	public void setValuablities(List<String> valuablities) {
		this.valuablities = valuablities;
	}

	public void setMinYear(int minYear) {
		this.minYear = minYear;
	}

	public void setMaxYear(int maxYear) {
		this.maxYear = maxYear;
	}
}
